package com.ksd.web.controller.upload;

import com.ksd.common.core.domain.AjaxResult;
import com.ksd.common.utils.file.FileUploadUtils;
import com.ksd.quartz.util.TaskConstans;
import com.ksd.system.domain.SysTvType;
import com.ksd.web.controller.domain.UploadFileObjec;
import com.ksd.web.controller.tool.XmlTypeJudge;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CheckXml {
//    public static AjaxResult checkFile(UploadFileObjec uploadFileObjec) {
//        AjaxResult ajaxResult = new AjaxResult();
//        List<String> uploadFilePaths = uploadFileObjec.getFilePaths();
//        List<String> filePaths = FileTreeController.filterFilePathsDir(uploadFilePaths);  //返回的是具体的文件夹
//        int totalFileSizes = 0;
//        for (String filePath : filePaths) {
//            File directory = new File(filePath);
//            if (directory.isDirectory()){
//                totalFileSizes = FileTreeController.findFilesInSubdirectories(directory,totalFileSizes);
//            }else{
//                if (FileUploadUtils.isAllowedExtension(directory.getName(), TaskConstans.XML_NAME)){
//                    //将文件夹下的所有文件都复制上传
//                    totalFileSizes++;
//                }
//            }
//        }
//        System.out.println("check xml total: " + totalFileSizes);
//        for (String filePath : filePaths) {
//            ajaxResult = checkEveryFileXml(filePath);
//            if (ajaxResult.isError()){
//                return ajaxResult;
//            }
//        }
//        return ajaxResult;
//    }

    public static AjaxResult checkEveryFileXml(String filePath) {
        AjaxResult ajaxResult = new AjaxResult();
        File file = new File(filePath);
        String name = file.getName();
        if (file.isDirectory()){
            File[] files = file.listFiles();
            System.out.println("files.length: " + files.length);
            for (File everyFile : files) {
                ajaxResult  = checkEveryFileXml(everyFile.getAbsolutePath());
            }
        }else{
            if (FileUploadUtils.isAllowedExtension(file.getName(), TaskConstans.XML_NAME)){
                System.out.println("checkEveryFileXml: " + file.getAbsolutePath());
                if (FileUploadUtils.isAllowedExtension(name,TaskConstans.MOVIE_XML_NAME)){
                    ajaxResult = XmlTypeJudge.judgeType(file.getAbsolutePath(), SysTvType.movieDefaultId);
                    if (ajaxResult.isError()){
                        return ajaxResult;
                    }
                }else if(FileUploadUtils.isAllowedExtension(name,TaskConstans.SONG_XML_NAME)){
                    ajaxResult = XmlTypeJudge.judgeType(file.getAbsolutePath(), SysTvType.songDefaultId);
                    if (ajaxResult.isError()){
                        return ajaxResult;
                    }
                }
            }
        }

        return ajaxResult;
    }


//    public static void main(String[] args) {
//        String path ="C:\\Users\\wuyun\\Desktop\\首页照片\\歌曲\\gfcyx";
////        String path2 ="C:\\Users\\wuyun\\Desktop\\首页照片\\电影";
//        UploadFileObjec uploadFileObjec = new UploadFileObjec();
//        List<String> filePaths = new ArrayList<String>();
////        filePaths.add(path);
//        filePaths.add(path);
//        uploadFileObjec.setFilePaths(filePaths);
//        AjaxResult ajaxResult = checkFile(uploadFileObjec);
//        System.out.println("结果:"+ajaxResult.toString());
//    }
}
